function [ s_start, Q ] = uct( mdp )
    % Inicializar Q a valores arbitrarios
    Q=zeros(mdp.num_states,length(mdp.actions));
    nsa=zeros(mdp.num_states,length(mdp.actions));
        
    num_simulaciones=100;
    max_steps=5000;
 
    s_start=calculate_puzzle_number([1     2     3     4     9     5     7     8     6]);%mdp.randomState();
    for sims=1:num_simulaciones
        steps=1;
        display(sims);
        tray = [];
        eoe = false;
        s=s_start;
        while(~eoe && steps<max_steps)
           a=chooseAction(Q, s,nsa,mdp);
           %a=chooseActionCorridor(Q, s,nsa,mdp);

           [s_next,r, eoe]=mdp.simularAmbiente(mdp,s,a);

           tray = [tray [s  a r]'];
           s=s_next;
           steps=steps+1;
        end
        %display(tray);
        if(eoe)
            for i=1:size(tray,2)
                sar =tray(:,i);
                s = sar(1);
                a = sar(2);
                R = tray(3,i:end);
                R = R* 0.9.^[1:length(R)]';

                nsa(s, a) = nsa(s, a)+1;
                Q(s, a) = Q(s, a) + 1/nsa(s,a) * (R - Q(s, a));
            end
        end        
    end

end

